package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveDate(@PathVariable String name,
                               @PathVariable int age,
                               @PathVariable String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        int i = reviewService.saveData(review);

        return i > 0 ? AjaxResult.success("成功插入：" + i + "条数据") : AjaxResult.error("插入失败");
    }

    @PostMapping("/saveData")
    public AjaxResult saveDate2(@RequestBody Review review) {


        int i = reviewService.saveData(review);

        return i > 0 ? AjaxResult.success("成功插入：" + i + "条数据") : AjaxResult.error("插入失败");
    }

    /**
     * =========================================================新增数据============================================
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteDate(@PathVariable Long id) {

        int i = reviewService.deleteDate(id);
        return i > 0 ? AjaxResult.success("成功插入：" + i + "条数据") : AjaxResult.error("插入失败");
    }

    /**
     * =========================================================删除数据=============================================
     */
    @PostMapping("/update")
    public AjaxResult updateDate(@RequestBody Review review) {
        MybatisReviewController mybatisReviewController = new MybatisReviewController();
        mybatisReviewController.selectById(review.getId());

        int i = reviewService.updateDate(review);
        return i > 0 ? AjaxResult.success("成功插入：" + i + "条数据") : AjaxResult.error("插入失败");
    }


    /**=========================================================修改数据=============================================*/

    /**
     * =========================================================查询数据=============================================
     */
    @GetMapping("/getById")
    public AjaxResult selectById(Long id) {

        Review review = reviewService.selectById(id);

        return AjaxResult.success("操作成功", review);
    }

    @GetMapping("/getDataByPage")
    public AjaxResult queryPages(Integer pageNum, Integer pageSize) {

        TableDataInfo tableDataInfo = reviewService.queryPage(pageNum, pageSize);

        return AjaxResult.success("查询成功", tableDataInfo);
    }


}